<?php
namespace app\admin\controller;

use think\console\command\make\Model;
use think\Db;

class System extends Base
{
	/**
     * @throws
	 * 设置首页
	 * @return [type] [description]
	 */
    public function index()
    {
        $model = Model("web_config");

        // 获取网站配置信息
        $web_config = $model->select();

        $config = [];

        // 格式化数据
        foreach ($web_config as $k => $v){

            if($v['type'] == 1){
                $config[$v['key']] = json_decode($v['value'],true);
            } else {
                $config[$v['key']] = $v['value'];
            }

        }

        $this->assign('config',$config);

		return $this->fetch();
	}


	/**
	 * 保存基础设置
     */
	public function saveBase()
    {
        $model = Model("web_config");

        if(request()->isPost()){
            $input = input();

            foreach ($input as $key => $val){

                $data['value'] = $val;

                $model->where('key',$key)->where('type',0)->update($data);

                unset($data);
            }

            // 记录日志
            $admin_log = new AdminLog();
            $admin_log->add('修改基本配置信息',$input,'edit');

        }

    }


    /**
     * 保存七牛云设置
     */
    public function saveQiniu()
    {
        $model = Model("web_config");

        if(request()->isPost()){
            $input = input();

            $data['value'] = json_encode($input);

            $re = $model->where('key','qiniu')->update($data);

            if($re){
                // 记录日志
                $admin_log = new AdminLog();
                $admin_log->add('修改七牛云设置',$input,'edit');

                $this->success("修改成功");
            }else{
                $this->error("网络异常");
            }

        }
    }


    /**
     * 保存微信配置
     */
    public function saveWchat()
    {
        $model = Model("web_config");

        if(request()->isPost()){
            $input = input();

            $data['value'] = json_encode($input);

            $re = $model->where('key','wchat')->update($data);

            if($re){
                // 记录日志
                $admin_log = new AdminLog();
                $admin_log->add('修改微信设置',$input,'edit');

                $this->success("修改成功");
            }else{
                $this->error("网络异常");
            }

        }
    }


    /**
     * 保存微信支付配置
     */
    public function saveWxPay()
    {
        $model = Model("web_config");

        if(request()->isPost()){
            $input = input();

            $data['value'] = json_encode($input);

            $re = $model->where('key','wxpay')->update($data);

            if($re){
                // 记录日志
                $admin_log = new AdminLog();
                $admin_log->add('修改微信支付设置',$input,'edit');

                $this->success("修改成功");
            }else{
                $this->error("网络异常");
            }

        }
    }
	
}
